package com.codeyang.mocktest2.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 描述: swagger2配置
 *
 * @author shengyang5
 * @version 2021/7/21 15:33
 */
@Configuration
@EnableSwagger2
//是否开启swagger，正式环境一般是需要关闭的（避免不必要的漏洞暴露！），可根据springboot的多环境配置进行设置
// 根据环境来激活是否加载该类
@Profile({"dev", "test"})
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 {

    //动态配置多环境是否开启
    @Value("${swagger2.enable}")
    private boolean swagger2Enable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger2Enable)
                .apiInfo(apiInfo())
                .select()
                //扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.codeyang.mocktest2"))
                .paths(PathSelectors.any())
                .build();
    }

    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("CodeYang")
                //描述
                .description("接口文档")
                //创建人
                // 创建人信息
                .contact(new Contact(
                        "CodeYang",
                        "https://www.baidu.com",
                        "ys1207575273@163.com"))
                //版本号
                .version("1.0")
                .build();
    }


}
